home *** CD-ROM | disk | FTP | other *** search
/ OpenGL Superbible (2nd Edition) / OpenGL SuperBible e2.iso / tools / Mesa-3.0 / SRC / CONTEXT.C < prev    next >
Encoding:
C/C++ Source or Header  |  1998-08-24  |  59.3 KB  |  2,078 lines

  1. /* $Id: context.c,v 3.21 1998/08/23 22:19:30 brianp Exp $ */
  2.  
  3. /*
  4.  * Mesa 3-D graphics library
  5.  * Version:  3.0
  6.  * Copyright (C) 1995-1998  Brian Paul
  7.  *
  8.  * This library is free software; you can redistribute it and/or
  9.  * modify it under the terms of the GNU Library General Public
  10.  * License as published by the Free Software Foundation; either
  11.  * version 2 of the License, or (at your option) any later version.
  12.  *
  13.  * This library is distributed in the hope that it will be useful,
  14.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  16.  * Library General Public License for more details.
  17.  *
  18.  * You should have received a copy of the GNU Library General Public
  19.  * License along with this library; if not, write to the Free
  20.  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  21.  */
  22.  
  23.  
  24. /*
  25.  * $Log: context.c,v $
  26.  * Revision 3.21  1998/08/23 22:19:30  brianp
  27.  * added DoViewportMapping to context struct
  28.  *
  29.  * Revision 3.20  1998/08/21 01:49:46  brianp
  30.  * initialize vertex array state
  31.  *
  32.  * Revision 3.19  1998/07/17 03:23:32  brianp
  33.  * added Pixel.ScaleOrBiasRGBA field
  34.  *
  35.  * Revision 3.18  1998/06/21 02:00:55  brianp
  36.  * cleaned up clip interpolation function code
  37.  *
  38.  * Revision 3.17  1998/06/19 03:13:10  brianp
  39.  * evaluator state wasn't fully initialized
  40.  *
  41.  * Revision 3.16  1998/06/07 22:18:52  brianp
  42.  * implemented GL_EXT_multitexture extension
  43.  *
  44.  * Revision 3.15  1998/04/01 02:58:28  brianp
  45.  * updated for v0.24 of 3Dfx/Glide driver
  46.  *
  47.  * Revision 3.14  1998/03/27 03:30:36  brianp
  48.  * fixed G++ warnings
  49.  *
  50.  * Revision 3.13  1998/03/22 16:42:22  brianp
  51.  * added 8-bit CI->RGBA pixel mapping tables
  52.  *
  53.  * Revision 3.12  1998/03/15 17:55:54  brianp
  54.  * added FogMode to context struct
  55.  *
  56.  * Revision 3.11  1998/03/05 02:49:48  brianp
  57.  * added an assertion and added better comments in gl_update_state()
  58.  *
  59.  * Revision 3.10  1998/03/01 20:17:03  brianp
  60.  * removed dead code
  61.  *
  62.  * Revision 3.9  1998/02/20 04:50:09  brianp
  63.  * implemented GL_SGIS_multitexture
  64.  *
  65.  * Revision 3.8  1998/02/13 04:38:05  brianp
  66.  * optimized blending functions called via BlendFunc function pointer
  67.  *
  68.  * Revision 3.7  1998/02/13 03:23:04  brianp
  69.  * AlphaRef is now a GLubyte
  70.  *
  71.  * Revision 3.6  1998/02/13 03:17:02  brianp
  72.  * added basic stereo support
  73.  *
  74.  * Revision 3.5  1998/02/08 20:20:34  brianp
  75.  * ColorMask is now GLubyte[4] instead of GLuint
  76.  *
  77.  * Revision 3.4  1998/02/05 01:10:25  brianp
  78.  * added John Stone's thread modifications
  79.  *
  80.  * Revision 3.3  1998/02/02 03:09:34  brianp
  81.  * added GL_LIGHT_MODEL_COLOR_CONTROL (separate specular color interpolation)
  82.  *
  83.  * Revision 3.2  1998/02/01 16:37:19  brianp
  84.  * added GL_EXT_rescale_normal extension
  85.  *
  86.  * Revision 3.1  1998/01/31 23:56:55  brianp
  87.  * removed Driver.ClearDepthBuffer function
  88.  *
  89.  * Revision 3.0  1998/01/31 20:49:07  brianp
  90.  * initial rev
  91.  *
  92.  */
  93.  
  94.  
  95. /*
  96.  * If multi-threading is enabled (-DTHREADS) then each thread has it's
  97.  * own rendering context.  A thread obtains the pointer to its GLcontext
  98.  * with the gl_get_thread_context() function.  Otherwise, the global
  99.  * pointer, CC, points to the current context used by all threads in
  100.  * the address space.
  101.  */
  102.  
  103.  
  104. #ifdef PC_HEADER
  105. #include "all.h"
  106. #else
  107. #include <assert.h>
  108. #include <math.h>
  109. #include <stdio.h>
  110. #include <stdlib.h>
  111. #include <string.h>
  112. #include "accum.h"
  113. #include "alphabuf.h"
  114. #include "clip.h"
  115. #include "context.h"
  116. #include "depth.h"
  117. #include "eval.h"
  118. #include "hash.h"
  119. #include "light.h"
  120. #include "lines.h"
  121. #include "dlist.h"
  122. #include "macros.h"
  123. #include "mmath.h"
  124. #include "pb.h"
  125. #include "points.h"
  126. #include "pointers.h"
  127. #include "quads.h"
  128. #include "stencil.h"
  129. #include "triangle.h"
  130. #include "teximage.h"
  131. #include "texobj.h"
  132. #include "texstate.h"
  133. #include "types.h"
  134. #include "vb.h"
  135. #include "vbfill.h"
  136. #endif
  137.  
  138.  
  139.  
  140. /**********************************************************************/
  141. /*****                  Context and Thread management             *****/
  142. /**********************************************************************/
  143.  
  144.  
  145. #ifdef THREADS
  146.  
  147. #include "mthreads.h" /* Mesa platform independent threads interface */
  148.  
  149. static MesaTSD mesa_ctx_tsd;
  150.  
  151. static void mesa_ctx_thread_init() {
  152.   MesaInitTSD(&mesa_ctx_tsd);
  153. }
  154.  
  155. GLcontext *gl_get_thread_context( void ) {
  156.   return (GLcontext *) MesaGetTSD(&mesa_ctx_tsd);
  157. }
  158.  
  159. static void set_thread_context( GLcontext *ctx ) {
  160.   MesaSetTSD(&mesa_ctx_tsd, ctx, mesa_ctx_thread_init);
  161. }
  162.  
  163.  
  164. #else
  165.  
  166. /* One Current Context pointer for all threads in the address space */
  167. GLcontext *CC = NULL;
  168.  
  169. #endif /*THREADS*/
  170.  
  171.  
  172.  
  173.  
  174. /**********************************************************************/
  175. /*****                   Profiling functions                      *****/
  176. /**********************************************************************/
  177.  
  178. #ifdef PROFILE
  179.  
  180. #include <sys/times.h>
  181. #include <sys/param.h>
  182.  
  183.  
  184. /*
  185.  * Return system time in seconds.
  186.  * NOTE:  this implementation may not be very portable!
  187.  */
  188. GLdouble gl_time( void )
  189. {
  190.    static GLdouble prev_time = 0.0;
  191.    static GLdouble time;
  192.    struct tms tm;
  193.    clock_t clk;
  194.  
  195.    clk = times(&tm);
  196.  
  197. #ifdef CLK_TCK
  198.    time = (double)clk / (double)CLK_TCK;
  199. #else
  200.    time = (double)clk / (double)HZ;
  201. #endif
  202.  
  203.    if (time>prev_time) {
  204.       prev_time = time;
  205.       return time;
  206.    }
  207.    else {
  208.       return prev_time;
  209.    }
  210. }
  211.  
  212.  
  213.  
  214. /*
  215.  * Reset the timing/profiling counters
  216.  */
  217. static void init_timings( GLcontext *ctx )
  218. {
  219.    ctx->BeginEndCount = 0;
  220.    ctx->BeginEndTime = 0.0;
  221.    ctx->VertexCount = 0;
  222.    ctx->VertexTime = 0.0;
  223.    ctx->PointCount = 0;
  224.    ctx->PointTime = 0.0;
  225.    ctx->LineCount = 0;
  226.    ctx->LineTime = 0.0;
  227.    ctx->PolygonCount = 0;
  228.    ctx->PolygonTime = 0.0;
  229.    ctx->ClearCount = 0;
  230.    ctx->ClearTime = 0.0;
  231.    ctx->SwapCount = 0;
  232.    ctx->SwapTime = 0.0;
  233. }
  234.  
  235.  
  236.  
  237. /*
  238.  * Print the accumulated timing/profiling data.
  239.  */
  240. static void print_timings( GLcontext *ctx )
  241. {
  242.    GLdouble beginendrate;
  243.    GLdouble vertexrate;
  244.    GLdouble pointrate;
  245.    GLdouble linerate;
  246.    GLdouble polygonrate;
  247.    GLdouble overhead;
  248.    GLdouble clearrate;
  249.    GLdouble swaprate;
  250.    GLdouble avgvertices;
  251.  
  252.    if (ctx->BeginEndTime>0.0) {
  253.       beginendrate = ctx->BeginEndCount / ctx->BeginEndTime;
  254.    }
  255.    else {
  256.       beginendrate = 0.0;
  257.    }
  258.    if (ctx->VertexTime>0.0) {
  259.       vertexrate = ctx->VertexCount / ctx->VertexTime;
  260.    }
  261.    else {
  262.       vertexrate = 0.0;
  263.    }
  264.    if (ctx->PointTime>0.0) {
  265.       pointrate = ctx->PointCount / ctx->PointTime;
  266.    }
  267.    else {
  268.       pointrate = 0.0;
  269.    }
  270.    if (ctx->LineTime>0.0) {
  271.       linerate = ctx->LineCount / ctx->LineTime;
  272.    }
  273.    else {
  274.       linerate = 0.0;
  275.    }
  276.    if (ctx->PolygonTime>0.0) {
  277.       polygonrate = ctx->PolygonCount / ctx->PolygonTime;
  278.    }
  279.    else {
  280.       polygonrate = 0.0;
  281.    }
  282.    if (ctx->ClearTime>0.0) {
  283.       clearrate = ctx->ClearCount / ctx->ClearTime;
  284.    }
  285.    else {
  286.       clearrate = 0.0;
  287.    }
  288.    if (ctx->SwapTime>0.0) {
  289.       swaprate = ctx->SwapCount / ctx->SwapTime;
  290.    }
  291.    else {
  292.       swaprate = 0.0;
  293.    }
  294.  
  295.    if (ctx->BeginEndCount>0) {
  296.       avgvertices = (GLdouble) ctx->VertexCount / (GLdouble) ctx->BeginEndCount;
  297.    }
  298.    else {
  299.       avgvertices = 0.0;
  300.    }
  301.  
  302.    overhead = ctx->BeginEndTime - ctx->VertexTime - ctx->PointTime
  303.               - ctx->LineTime - ctx->PolygonTime;
  304.  
  305.  
  306.    printf("                          Count   Time (s)    Rate (/s) \n");
  307.    printf("--------------------------------------------------------\n");
  308.    printf("glBegin/glEnd           %7d  %8.3f   %10.3f\n",
  309.           ctx->BeginEndCount, ctx->BeginEndTime, beginendrate);
  310.    printf("  vertexes transformed  %7d  %8.3f   %10.3f\n",
  311.           ctx->VertexCount, ctx->VertexTime, vertexrate );
  312.    printf("  points rasterized     %7d  %8.3f   %10.3f\n",
  313.           ctx->PointCount, ctx->PointTime, pointrate );
  314.    printf("  lines rasterized      %7d  %8.3f   %10.3f\n",
  315.           ctx->LineCount, ctx->LineTime, linerate );
  316.    printf("  polygons rasterized   %7d  %8.3f   %10.3f\n",
  317.           ctx->PolygonCount, ctx->PolygonTime, polygonrate );
  318.    printf("  overhead                       %8.3f\n", overhead );
  319.    printf("glClear                 %7d  %8.3f   %10.3f\n",
  320.           ctx->ClearCount, ctx->ClearTime, clearrate );
  321.    printf("SwapBuffers             %7d  %8.3f   %10.3f\n",
  322.           ctx->SwapCount, ctx->SwapTime, swaprate );
  323.    printf("\n");
  324.  
  325.    printf("Average number of vertices per begin/end: %8.3f\n", avgvertices );
  326. }
  327. #endif
  328.  
  329.  
  330.  
  331.  
  332.  
  333. /**********************************************************************/
  334. /*****       Context allocation, initialization, destroying       *****/
  335. /**********************************************************************/
  336.  
  337.  
  338. /*
  339.  * Allocate and initialize a shared context state structure.
  340.  */
  341. static struct gl_shared_state *alloc_shared_state( void )
  342. {
  343.    GLuint i;
  344.    struct gl_shared_state *ss;
  345.    GLboolean outOfMemory;
  346.  
  347.    ss = (struct gl_shared_state*) calloc( 1, sizeof(struct gl_shared_state) );
  348.    if (!ss)
  349.       return NULL;
  350.  
  351.    ss->DisplayList = NewHashTable();
  352.  
  353.    ss->TexObjects = NewHashTable();
  354.  
  355.    /* Default Texture objects */
  356.    outOfMemory = GL_FALSE;
  357.    for (i=0;i<MAX_TEX_SETS;i++) {
  358.       ss->Default1D[i] = gl_alloc_texture_object(ss, 0, 1);
  359.       ss->Default2D[i] = gl_alloc_texture_object(ss, 0, 2);
  360.       ss->Default3D[i] = gl_alloc_texture_object(ss, 0, 3);
  361.       if (!ss->Default1D[i] || !ss->Default2D[i] || !ss->Default3D[i]) {
  362.          outOfMemory = GL_TRUE;
  363.          break;
  364.       }
  365.    }
  366.  
  367.    if (!ss->DisplayList || !ss->TexObjects || outOfMemory) {
  368.       /* Ran out of memory at some point.  Free everything and return NULL */
  369.       if (!ss->DisplayList)
  370.          DeleteHashTable(ss->DisplayList);
  371.       if (!ss->TexObjects)
  372.          DeleteHashTable(ss->TexObjects);
  373.       for (i=0;i<MAX_TEX_SETS;i++) {
  374.          if (!ss->Default1D[i])
  375.             gl_free_texture_object(ss, ss->Default1D[i]);
  376.          if (!ss->Default2D[i])
  377.             gl_free_texture_object(ss, ss->Default2D[i]);
  378.          if (!ss->Default3D[i])
  379.             gl_free_texture_object(ss, ss->Default3D[i]);
  380.       }
  381.       free(ss);
  382.       return NULL;
  383.    }
  384.    else {
  385.       return ss;
  386.    }
  387. }
  388.  
  389.  
  390. /*
  391.  * Deallocate a shared state context and all children structures.
  392.  */
  393. static void free_shared_state( GLcontext *ctx, struct gl_shared_state *ss )
  394. {
  395.    /* Free display lists */
  396.    while (1) {
  397.       GLuint list = HashFirstEntry(ss->DisplayList);
  398.       if (list) {
  399.          gl_destroy_list(ctx, list);
  400.       }
  401.       else {
  402.          break;
  403.       }
  404.    }
  405.    DeleteHashTable(ss->DisplayList);
  406.  
  407.    /* Free texture objects */
  408.    while (ss->TexObjectList)
  409.    {
  410.       /* this function removes from linked list too! */
  411.       gl_free_texture_object(ss, ss->TexObjectList);
  412.    }
  413.    DeleteHashTable(ss->TexObjects);
  414.  
  415.    free(ss);
  416. }
  417.  
  418.  
  419.  
  420.  
  421. /*
  422.  * Initialize the nth light.  Note that the defaults for light 0 are
  423.  * different than the other lights.
  424.  */
  425. static void init_light( struct gl_light *l, GLuint n )
  426. {
  427.    ASSIGN_4V( l->Ambient, 0.0, 0.0, 0.0, 1.0 );
  428.    if (n==0) {
  429.       ASSIGN_4V( l->Diffuse, 1.0, 1.0, 1.0, 1.0 );
  430.       ASSIGN_4V( l->Specular, 1.0, 1.0, 1.0, 1.0 );
  431.    }
  432.    else {
  433.       ASSIGN_4V( l->Diffuse, 0.0, 0.0, 0.0, 1.0 );
  434.       ASSIGN_4V( l->Specular, 0.0, 0.0, 0.0, 1.0 );
  435.    }
  436.    ASSIGN_4V( l->Position, 0.0, 0.0, 1.0, 0.0 );
  437.    ASSIGN_3V( l->Direction, 0.0, 0.0, -1.0 );
  438.    l->SpotExponent = 0.0;
  439.    gl_compute_spot_exp_table( l );
  440.    l->SpotCutoff = 180.0;
  441.    l->CosCutoff = -1.0;
  442.    l->ConstantAttenuation = 1.0;
  443.    l->LinearAttenuation = 0.0;
  444.    l->QuadraticAttenuation = 0.0;
  445.    l->Enabled = GL_FALSE;
  446. }
  447.  
  448.  
  449.  
  450. static void init_lightmodel( struct gl_lightmodel *lm )
  451. {
  452.    ASSIGN_4V( lm->Ambient, 0.2, 0.2, 0.2, 1.0 );
  453.    lm->LocalViewer = GL_FALSE;
  454.    lm->TwoSide = GL_FALSE;
  455.    lm->ColorControl = GL_SINGLE_COLOR;
  456. }
  457.  
  458.  
  459. static void init_material( struct gl_material *m )
  460. {
  461.    ASSIGN_4V( m->Ambient,  0.2, 0.2, 0.2, 1.0 );
  462.    ASSIGN_4V( m->Diffuse,  0.8, 0.8, 0.8, 1.0 );
  463.    ASSIGN_4V( m->Specular, 0.0, 0.0, 0.0, 1.0 );
  464.    ASSIGN_4V( m->Emission, 0.0, 0.0, 0.0, 1.0 );
  465.    m->Shininess = 0.0;
  466.    m->AmbientIndex = 0;
  467.    m->DiffuseIndex = 1;
  468.    m->SpecularIndex = 1;
  469.    gl_compute_material_shine_table( m );
  470. }
  471.  
  472.  
  473. static void init_texture_set( GLcontext *ctx, GLuint setNum )
  474. {
  475.    struct gl_texture_set *s = &ctx->Texture.Set[setNum];
  476.  
  477.    s->EnvMode = GL_MODULATE;
  478.    ASSIGN_4V( s->EnvColor, 0.0, 0.0, 0.0, 0.0 );
  479.    s->TexGenEnabled = 0;
  480.    s->GenModeS = GL_EYE_LINEAR;
  481.    s->GenModeT = GL_EYE_LINEAR;
  482.    s->GenModeR = GL_EYE_LINEAR;
  483.    s->GenModeQ = GL_EYE_LINEAR;
  484.    /* Yes, these plane coefficients are correct! */
  485.    ASSIGN_4V( s->ObjectPlaneS, 1.0, 0.0, 0.0, 0.0 );
  486.    ASSIGN_4V( s->ObjectPlaneT, 0.0, 1.0, 0.0, 0.0 );
  487.    ASSIGN_4V( s->ObjectPlaneR, 0.0, 0.0, 0.0, 0.0 );
  488.    ASSIGN_4V( s->ObjectPlaneQ, 0.0, 0.0, 0.0, 0.0 );
  489.    ASSIGN_4V( s->EyePlaneS, 1.0, 0.0, 0.0, 0.0 );
  490.    ASSIGN_4V( s->EyePlaneT, 0.0, 1.0, 0.0, 0.0 );
  491.    ASSIGN_4V( s->EyePlaneR, 0.0, 0.0, 0.0, 0.0 );
  492.    ASSIGN_4V( s->EyePlaneQ, 0.0, 0.0, 0.0, 0.0 );
  493.  
  494.    s->Current1D = ctx->Shared->Default1D[setNum];
  495.    s->Current2D = ctx->Shared->Default2D[setNum];
  496.    s->Current3D = ctx->Shared->Default3D[setNum];
  497.  
  498.    s->TexCoordSet = setNum;   /* GL_EXT_multitexture */
  499. }
  500.  
  501.  
  502. /* Initialize a 1-D evaluator map */
  503. static void init_1d_map( struct gl_1d_map *map, int n, const float *initial )
  504. {
  505.    map->Order = 1;
  506.    map->u1 = 0.0;
  507.    map->u2 = 1.0;
  508.    map->Points = malloc(n * sizeof(GLfloat));
  509.    if (map->Points) {
  510.       GLint i;
  511.       for (i=0;i<n;i++)
  512.          map->Points[i] = initial[i];
  513.    }
  514.    map->Retain = GL_FALSE;
  515. }
  516.  
  517.  
  518. /* Initialize a 2-D evaluator map */
  519. static void init_2d_map( struct gl_2d_map *map, int n, const float *initial )
  520. {
  521.    map->Uorder = 1;
  522.    map->Vorder = 1;
  523.    map->u1 = 0.0;
  524.    map->u2 = 1.0;
  525.    map->v1 = 0.0;
  526.    map->v2 = 1.0;
  527.    map->Points = malloc(n * sizeof(GLfloat));
  528.    if (map->Points) {
  529.       GLint i;
  530.       for (i=0;i<n;i++)
  531.          map->Points[i] = initial[i];
  532.    }
  533.    map->Retain = GL_FALSE;
  534. }
  535.  
  536.  
  537.  
  538. /*
  539.  * Initialize a gl_context structure to default values.
  540.  */
  541. static void initialize_context( GLcontext *ctx )
  542. {
  543.    static GLfloat identity[16] = {
  544.     1.0, 0.0, 0.0, 0.0,
  545.     0.0, 1.0, 0.0, 0.0,
  546.     0.0, 0.0, 1.0, 0.0,
  547.     0.0, 0.0, 0.0, 1.0
  548.    };
  549.    GLuint i;
  550.  
  551.    if (ctx) {
  552.       /* Modelview matrix */
  553.       ctx->NewModelViewMatrix = GL_FALSE;
  554.       ctx->ModelViewMatrixType = MATRIX_IDENTITY;
  555.       MEMCPY( ctx->ModelViewMatrix, identity, 16*sizeof(GLfloat) );
  556.       MEMCPY( ctx->ModelViewInv, identity, 16*sizeof(GLfloat) );
  557.       ctx->ModelViewStackDepth = 0;
  558.  
  559.       /* Projection matrix */
  560.       ctx->NewProjectionMatrix = GL_FALSE;
  561.       ctx->ProjectionMatrixType = MATRIX_IDENTITY;
  562.       MEMCPY( ctx->ProjectionMatrix, identity, 16*sizeof(GLfloat) );
  563.       ctx->ProjectionStackDepth = 0;
  564.       ctx->NearFarStack[0][0] = 1.0; /* These values seem weird by make */
  565.       ctx->NearFarStack[0][1] = 0.0; /* sense mathematically. */
  566.  
  567.       /* Texture matrix */
  568.       for (i=0; i<MAX_TEX_SETS; i++) {
  569.          ctx->NewTextureMatrix = GL_FALSE;
  570.          ctx->TextureMatrixType[i] = MATRIX_IDENTITY;
  571.          MEMCPY( ctx->TextureMatrix[i], identity, 16*sizeof(GLfloat) );
  572.          ctx->TextureStackDepth[i] = 0;
  573.       }
  574.  
  575.       /* Accumulate buffer group */
  576.       ASSIGN_4V( ctx->Accum.ClearColor, 0.0, 0.0, 0.0, 0.0 );
  577.  
  578.       /* Color buffer group */
  579.       ctx->Color.IndexMask = 0xffffffff;
  580.       ctx->Color.ColorMask[0] = 0xff;
  581.       ctx->Color.ColorMask[1] = 0xff;
  582.       ctx->Color.ColorMask[2] = 0xff;
  583.       ctx->Color.ColorMask[3] = 0xff;
  584.       ctx->Color.SWmasking = GL_FALSE;
  585.       ctx->Color.ClearIndex = 0;
  586.       ASSIGN_4V( ctx->Color.ClearColor, 0.0, 0.0, 0.0, 0.0 );
  587.       ctx->Color.DrawBuffer = GL_FRONT;
  588.       ctx->Color.AlphaEnabled = GL_FALSE;
  589.       ctx->Color.AlphaFunc = GL_ALWAYS;
  590.       ctx->Color.AlphaRef = 0;
  591.       ctx->Color.BlendEnabled = GL_FALSE;
  592.       ctx->Color.BlendSrc = GL_ONE;
  593.       ctx->Color.BlendDst = GL_ZERO;
  594.       ctx->Color.BlendEquation = GL_FUNC_ADD_EXT;
  595.       ctx->Color.BlendFunc = NULL;  /* this pointer set only when needed */
  596.       ASSIGN_4V( ctx->Color.BlendColor, 0.0, 0.0, 0.0, 0.0 );
  597.       ctx->Color.IndexLogicOpEnabled = GL_FALSE;
  598.       ctx->Color.ColorLogicOpEnabled = GL_FALSE;
  599.       ctx->Color.SWLogicOpEnabled = GL_FALSE;
  600.       ctx->Color.LogicOp = GL_COPY;
  601.       ctx->Color.DitherFlag = GL_TRUE;
  602.  
  603.       /* Current group */
  604.       ASSIGN_4V( ctx->Current.ByteColor, 255, 255, 255, 255 );
  605.       ctx->Current.Index = 1;
  606.       ASSIGN_3V( ctx->Current.Normal, 0.0, 0.0, 1.0 );
  607.       for (i=0; i<MAX_TEX_SETS; i++)
  608.          ASSIGN_4V( ctx->Current.MultiTexCoord[i], 0.0, 0.0, 0.0, 1.0 );
  609.       ctx->Current.TexCoord = ctx->Current.MultiTexCoord[0];
  610.       ASSIGN_4V( ctx->Current.RasterPos, 0.0, 0.0, 0.0, 1.0 );
  611.       ctx->Current.RasterDistance = 0.0;
  612.       ASSIGN_4V( ctx->Current.RasterColor, 1.0, 1.0, 1.0, 1.0 );
  613.       ctx->Current.RasterIndex = 1;
  614.       for (i=0; i<MAX_TEX_SETS; i++)
  615.          ASSIGN_4V( ctx->Current.RasterMultiTexCoord[i], 0.0, 0.0, 0.0, 1.0 );
  616.       ctx->Current.RasterTexCoord = ctx->Current.RasterMultiTexCoord[0];
  617.       ctx->Current.RasterPosValid = GL_TRUE;
  618.       ctx->Current.EdgeFlag = GL_TRUE;
  619.  
  620.       /* Depth buffer group */
  621.       ctx->Depth.Test = GL_FALSE;
  622.       ctx->Depth.Clear = 1.0;
  623.       ctx->Depth.Func = GL_LESS;
  624.       ctx->Depth.Mask = GL_TRUE;
  625.  
  626.       /* Evaluators group */
  627.       ctx->Eval.Map1Color4 = GL_FALSE;
  628.       ctx->Eval.Map1Index = GL_FALSE;
  629.       ctx->Eval.Map1Normal = GL_FALSE;
  630.       ctx->Eval.Map1TextureCoord1 = GL_FALSE;
  631.       ctx->Eval.Map1TextureCoord2 = GL_FALSE;
  632.       ctx->Eval.Map1TextureCoord3 = GL_FALSE;
  633.       ctx->Eval.Map1TextureCoord4 = GL_FALSE;
  634.       ctx->Eval.Map1Vertex3 = GL_FALSE;
  635.       ctx->Eval.Map1Vertex4 = GL_FALSE;
  636.       ctx->Eval.Map2Color4 = GL_FALSE;
  637.       ctx->Eval.Map2Index = GL_FALSE;
  638.       ctx->Eval.Map2Normal = GL_FALSE;
  639.       ctx->Eval.Map2TextureCoord1 = GL_FALSE;
  640.       ctx->Eval.Map2TextureCoord2 = GL_FALSE;
  641.       ctx->Eval.Map2TextureCoord3 = GL_FALSE;
  642.       ctx->Eval.Map2TextureCoord4 = GL_FALSE;
  643.       ctx->Eval.Map2Vertex3 = GL_FALSE;
  644.       ctx->Eval.Map2Vertex4 = GL_FALSE;
  645.       ctx->Eval.AutoNormal = GL_FALSE;
  646.       ctx->Eval.MapGrid1un = 1;
  647.       ctx->Eval.MapGrid1u1 = 0.0;
  648.       ctx->Eval.MapGrid1u2 = 1.0;
  649.       ctx->Eval.MapGrid2un = 1;
  650.       ctx->Eval.MapGrid2vn = 1;
  651.       ctx->Eval.MapGrid2u1 = 0.0;
  652.       ctx->Eval.MapGrid2u2 = 1.0;
  653.       ctx->Eval.MapGrid2v1 = 0.0;
  654.       ctx->Eval.MapGrid2v2 = 1.0;
  655.  
  656.       /* Evaluator data */
  657.       {
  658.          static GLfloat vertex[4] = { 0.0, 0.0, 0.0, 1.0 };
  659.          static GLfloat normal[3] = { 0.0, 0.0, 1.0 };
  660.          static GLfloat index[1] = { 1.0 };
  661.          static GLfloat color[4] = { 1.0, 1.0, 1.0, 1.0 };
  662.          static GLfloat texcoord[4] = { 0.0, 0.0, 0.0, 1.0 };
  663.  
  664.          init_1d_map( &ctx->EvalMap.Map1Vertex3, 3, vertex );
  665.          init_1d_map( &ctx->EvalMap.Map1Vertex4, 4, vertex );
  666.          init_1d_map( &ctx->EvalMap.Map1Index, 1, index );
  667.          init_1d_map( &ctx->EvalMap.Map1Color4, 4, color );
  668.          init_1d_map( &ctx->EvalMap.Map1Normal, 3, normal );
  669.          init_1d_map( &ctx->EvalMap.Map1Texture1, 1, texcoord );
  670.          init_1d_map( &ctx->EvalMap.Map1Texture2, 2, texcoord );
  671.          init_1d_map( &ctx->EvalMap.Map1Texture3, 3, texcoord );
  672.          init_1d_map( &ctx->EvalMap.Map1Texture4, 4, texcoord );
  673.  
  674.          init_2d_map( &ctx->EvalMap.Map2Vertex3, 3, vertex );
  675.          init_2d_map( &ctx->EvalMap.Map2Vertex4, 4, vertex );
  676.          init_2d_map( &ctx->EvalMap.Map2Index, 1, index );
  677.          init_2d_map( &ctx->EvalMap.Map2Color4, 4, color );
  678.          init_2d_map( &ctx->EvalMap.Map2Normal, 3, normal );
  679.          init_2d_map( &ctx->EvalMap.Map2Texture1, 1, texcoord );
  680.          init_2d_map( &ctx->EvalMap.Map2Texture2, 2, texcoord );
  681.          init_2d_map( &ctx->EvalMap.Map2Texture3, 3, texcoord );
  682.          init_2d_map( &ctx->EvalMap.Map2Texture4, 4, texcoord );
  683.       }
  684.  
  685.       /* Fog group */
  686.       ctx->Fog.Enabled = GL_FALSE;
  687.       ctx->Fog.Mode = GL_EXP;
  688.       ASSIGN_4V( ctx->Fog.Color, 0.0, 0.0, 0.0, 0.0 );
  689.       ctx->Fog.Index = 0.0;
  690.       ctx->Fog.Density = 1.0;
  691.       ctx->Fog.Start = 0.0;
  692.       ctx->Fog.End = 1.0;
  693.  
  694.       /* Hint group */
  695.       ctx->Hint.PerspectiveCorrection = GL_DONT_CARE;
  696.       ctx->Hint.PointSmooth = GL_DONT_CARE;
  697.       ctx->Hint.LineSmooth = GL_DONT_CARE;
  698.       ctx->Hint.PolygonSmooth = GL_DONT_CARE;
  699.       ctx->Hint.Fog = GL_DONT_CARE;
  700.  
  701.       /* Lighting group */
  702.       for (i=0;i<MAX_LIGHTS;i++) {
  703.      init_light( &ctx->Light.Light[i], i );
  704.       }
  705.       init_lightmodel( &ctx->Light.Model );
  706.       init_material( &ctx->Light.Material[0] );
  707.       init_material( &ctx->Light.Material[1] );
  708.       ctx->Light.ShadeModel = GL_SMOOTH;
  709.       ctx->Light.Enabled = GL_FALSE;
  710.       ctx->Light.ColorMaterialFace = GL_FRONT_AND_BACK;
  711.       ctx->Light.ColorMaterialMode = GL_AMBIENT_AND_DIFFUSE;
  712.       ctx->Light.ColorMaterialBitmask
  713.          = gl_material_bitmask( GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE );
  714.  
  715.       ctx->Light.ColorMaterialEnabled = GL_FALSE;
  716.  
  717.       /* Line group */
  718.       ctx->Line.SmoothFlag = GL_FALSE;
  719.       ctx->Line.StippleFlag = GL_FALSE;
  720.       ctx->Line.Width = 1.0;
  721.       ctx->Line.StipplePattern = 0xffff;
  722.       ctx->Line.StippleFactor = 1;
  723.  
  724.       /* Display List group */
  725.       ctx->List.ListBase = 0;
  726.  
  727.       /* Pixel group */
  728.       ctx->Pixel.RedBias = 0.0;
  729.       ctx->Pixel.RedScale = 1.0;
  730.       ctx->Pixel.GreenBias = 0.0;
  731.       ctx->Pixel.GreenScale = 1.0;
  732.       ctx->Pixel.BlueBias = 0.0;
  733.       ctx->Pixel.BlueScale = 1.0;
  734.       ctx->Pixel.AlphaBias = 0.0;
  735.       ctx->Pixel.AlphaScale = 1.0;
  736.       ctx->Pixel.ScaleOrBiasRGBA = GL_FALSE;
  737.       ctx->Pixel.DepthBias = 0.0;
  738.       ctx->Pixel.DepthScale = 1.0;
  739.       ctx->Pixel.IndexOffset = 0;
  740.       ctx->Pixel.IndexShift = 0;
  741.       ctx->Pixel.ZoomX = 1.0;
  742.       ctx->Pixel.ZoomY = 1.0;
  743.       ctx->Pixel.MapColorFlag = GL_FALSE;
  744.       ctx->Pixel.MapStencilFlag = GL_FALSE;
  745.       ctx->Pixel.MapStoSsize = 1;
  746.       ctx->Pixel.MapItoIsize = 1;
  747.       ctx->Pixel.MapItoRsize = 1;
  748.       ctx->Pixel.MapItoGsize = 1;
  749.       ctx->Pixel.MapItoBsize = 1;
  750.       ctx->Pixel.MapItoAsize = 1;
  751.       ctx->Pixel.MapRtoRsize = 1;
  752.       ctx->Pixel.MapGtoGsize = 1;
  753.       ctx->Pixel.MapBtoBsize = 1;
  754.       ctx->Pixel.MapAtoAsize = 1;
  755.       ctx->Pixel.MapStoS[0] = 0;
  756.       ctx->Pixel.MapItoI[0] = 0;
  757.       ctx->Pixel.MapItoR[0] = 0.0;
  758.       ctx->Pixel.MapItoG[0] = 0.0;
  759.       ctx->Pixel.MapItoB[0] = 0.0;
  760.       ctx->Pixel.MapItoA[0] = 0.0;
  761.       ctx->Pixel.MapItoR8[0] = 0;
  762.       ctx->Pixel.MapItoG8[0] = 0;
  763.       ctx->Pixel.MapItoB8[0] = 0;
  764.       ctx->Pixel.MapItoA8[0] = 0;
  765.       ctx->Pixel.MapRtoR[0] = 0.0;
  766.       ctx->Pixel.MapGtoG[0] = 0.0;
  767.       ctx->Pixel.MapBtoB[0] = 0.0;
  768.       ctx->Pixel.MapAtoA[0] = 0.0;
  769.  
  770.       /* Point group */
  771.       ctx->Point.SmoothFlag = GL_FALSE;
  772.       ctx->Point.Size = 1.0;
  773.       ctx->Point.Params[0] = 1.0;
  774.       ctx->Point.Params[1] = 0.0;
  775.       ctx->Point.Params[2] = 0.0;
  776.       ctx->Point.MinSize = 0.0;
  777.       ctx->Point.MaxSize = (GLfloat) MAX_POINT_SIZE;
  778.       ctx->Point.Threshold = 1.0;
  779.  
  780.       /* Polygon group */
  781.       ctx->Polygon.CullFlag = GL_FALSE;
  782.       ctx->Polygon.CullFaceMode = GL_BACK;
  783.       ctx->Polygon.FrontFace = GL_CCW;
  784.       ctx->Polygon.FrontMode = GL_FILL;
  785.       ctx->Polygon.BackMode = GL_FILL;
  786.       ctx->Polygon.Unfilled = GL_FALSE;
  787.       ctx->Polygon.SmoothFlag = GL_FALSE;
  788.       ctx->Polygon.StippleFlag = GL_FALSE;
  789.       ctx->Polygon.OffsetFactor = 0.0F;
  790.       ctx->Polygon.OffsetUnits = 0.0F;
  791.       ctx->Polygon.OffsetPoint = GL_FALSE;
  792.       ctx->Polygon.OffsetLine = GL_FALSE;
  793.       ctx->Polygon.OffsetFill = GL_FALSE;
  794.       ctx->Polygon.OffsetAny = GL_FALSE;
  795.  
  796.       /* Polygon Stipple group */
  797.       MEMSET( ctx->PolygonStipple, 0xff, 32*sizeof(GLuint) );
  798.  
  799.       /* Scissor group */
  800.       ctx->Scissor.Enabled = GL_FALSE;
  801.       ctx->Scissor.X = 0;
  802.       ctx->Scissor.Y = 0;
  803.       ctx->Scissor.Width = 0;
  804.       ctx->Scissor.Height = 0;
  805.  
  806.       /* Stencil group */
  807.       ctx->Stencil.Enabled = GL_FALSE;
  808.       ctx->Stencil.Function = GL_ALWAYS;
  809.       ctx->Stencil.FailFunc = GL_KEEP;
  810.       ctx->Stencil.ZPassFunc = GL_KEEP;
  811.       ctx->Stencil.ZFailFunc = GL_KEEP;
  812.       ctx->Stencil.Ref = 0;
  813.       ctx->Stencil.ValueMask = 0xff;
  814.       ctx->Stencil.Clear = 0;
  815.       ctx->Stencil.WriteMask = 0xff;
  816.  
  817.       /* Texture group */
  818.       ctx->Texture.CurrentSet = 0;      /* GL_SGIS_multitexture */
  819.       ctx->Texture.CurrentTransformSet = 0; /* GL_EXT_multitexture */
  820.       ctx->Texture.Enabled = 0;
  821.  
  822.       ctx->Texture.AnyDirty = GL_FALSE;
  823.       for (i=0; i<MAX_TEX_SETS; i++)
  824.          init_texture_set( ctx, i );
  825.  
  826.       ctx->Texture.SharedPalette = GL_FALSE;
  827.       ctx->Texture.Palette[0] = 255;
  828.       ctx->Texture.Palette[1] = 255;
  829.       ctx->Texture.Palette[2] = 255;
  830.       ctx->Texture.Palette[3] = 255;
  831.       ctx->Texture.PaletteSize = 1;
  832.       ctx->Texture.PaletteIntFormat = GL_RGBA;
  833.       ctx->Texture.PaletteFormat = GL_RGBA;
  834.  
  835.       /* Transformation group */
  836.       ctx->Transform.MatrixMode = GL_MODELVIEW;
  837.       ctx->Transform.Normalize = GL_FALSE;
  838.       ctx->Transform.RescaleNormals = GL_FALSE;
  839.       for (i=0;i<MAX_CLIP_PLANES;i++) {
  840.      ctx->Transform.ClipEnabled[i] = GL_FALSE;
  841.          ASSIGN_4V( ctx->Transform.ClipEquation[i], 0.0, 0.0, 0.0, 0.0 );
  842.       }
  843.       ctx->Transform.AnyClip = GL_FALSE;
  844.  
  845.       /* Viewport group */
  846.       ctx->Viewport.X = 0;
  847.       ctx->Viewport.Y = 0;
  848.       ctx->Viewport.Width = 0;
  849.       ctx->Viewport.Height = 0;   
  850.       ctx->Viewport.Near = 0.0;
  851.       ctx->Viewport.Far = 1.0;
  852.       ctx->Viewport.Sx = 0.0;  /* Sx, Tx, Sy, Ty are computed later */
  853.       ctx->Viewport.Tx = 0.0;
  854.       ctx->Viewport.Sy = 0.0;
  855.       ctx->Viewport.Ty = 0.0;
  856.       ctx->Viewport.Sz = 0.5 * DEPTH_SCALE;
  857.       ctx->Viewport.Tz = 0.5 * DEPTH_SCALE;
  858.  
  859.       /* Vertex arrays */
  860.       ctx->Array.VertexSize = 4;
  861.       ctx->Array.VertexType = GL_FLOAT;
  862.       ctx->Array.VertexStride = 0;
  863.       ctx->Array.VertexStrideB = 0;
  864.       ctx->Array.VertexPtr = NULL;
  865.       ctx->Array.VertexEnabled = GL_FALSE;
  866.       ctx->Array.NormalType = GL_FLOAT;
  867.       ctx->Array.NormalStride = 0;
  868.       ctx->Array.NormalStrideB = 0;
  869.       ctx->Array.NormalPtr = NULL;
  870.       ctx->Array.NormalEnabled = GL_FALSE;
  871.       ctx->Array.ColorSize = 4;
  872.       ctx->Array.ColorType = GL_FLOAT;
  873.       ctx->Array.ColorStride = 0;
  874.       ctx->Array.ColorStrideB = 0;
  875.       ctx->Array.ColorPtr = NULL;
  876.       ctx->Array.ColorEnabled = GL_FALSE;
  877.       ctx->Array.IndexType = GL_FLOAT;
  878.       ctx->Array.IndexStride = 0;
  879.       ctx->Array.IndexStrideB = 0;
  880.       ctx->Array.IndexPtr = NULL;
  881.       ctx->Array.IndexEnabled = GL_FALSE;
  882.       for (i = 0; i < MAX_TEX_COORD_SETS; i++) {
  883.          ctx->Array.TexCoordSize[i] = 4;
  884.          ctx->Array.TexCoordType[i] = GL_FLOAT;
  885.          ctx->Array.TexCoordStride[i] = 0;
  886.          ctx->Array.TexCoordStrideB[i] = 0;
  887.          ctx->Array.TexCoordPtr[i] = NULL;
  888.          ctx->Array.TexCoordEnabled[i] = GL_FALSE;
  889.       }
  890.       ctx->Array.TexCoordInterleaveFactor = 1;
  891.       ctx->Array.EdgeFlagStride = 0;
  892.       ctx->Array.EdgeFlagStrideB = 0;
  893.       ctx->Array.EdgeFlagPtr = NULL;
  894.       ctx->Array.EdgeFlagEnabled = GL_FALSE;
  895.  
  896.       /* Pixel transfer */
  897.       ctx->Pack.Alignment = 4;
  898.       ctx->Pack.RowLength = 0;
  899.       ctx->Pack.SkipPixels = 0;
  900.       ctx->Pack.SkipRows = 0;
  901.       ctx->Pack.SwapBytes = GL_FALSE;
  902.       ctx->Pack.LsbFirst = GL_FALSE;
  903.       ctx->Unpack.Alignment = 4;
  904.       ctx->Unpack.RowLength = 0;
  905.       ctx->Unpack.SkipPixels = 0;
  906.       ctx->Unpack.SkipRows = 0;
  907.       ctx->Unpack.SwapBytes = GL_FALSE;
  908.       ctx->Unpack.LsbFirst = GL_FALSE;
  909.  
  910.       /* Feedback */
  911.       ctx->Feedback.Type = GL_2D;   /* TODO: verify */
  912.       ctx->Feedback.Buffer = NULL;
  913.       ctx->Feedback.BufferSize = 0;
  914.       ctx->Feedback.Count = 0;
  915.  
  916.       /* Selection/picking */
  917.       ctx->Select.Buffer = NULL;
  918.       ctx->Select.BufferSize = 0;
  919.       ctx->Select.BufferCount = 0;
  920.       ctx->Select.Hits = 0;
  921.       ctx->Select.NameStackDepth = 0;
  922.  
  923.       /* GL_EXT_multitexture */
  924.       ctx->TexCoordSet = 0;
  925.  
  926.       /* Renderer and client attribute stacks */
  927.       ctx->AttribStackDepth = 0;
  928.       ctx->ClientAttribStackDepth = 0;
  929.  
  930.       /*** Miscellaneous ***/
  931.       ctx->NewState = NEW_ALL;
  932.       ctx->RenderMode = GL_RENDER;
  933.       ctx->Primitive = GL_BITMAP;
  934.       ctx->StippleCounter = 0;
  935.       ctx->NeedNormals = GL_FALSE;
  936.       ctx->DoViewportMapping = GL_TRUE;
  937.  
  938.       /* Display list */
  939.       ctx->CallDepth = 0;
  940.       ctx->ExecuteFlag = GL_TRUE;
  941.       ctx->CompileFlag = GL_FALSE;
  942.       ctx->CurrentListPtr = NULL;
  943.       ctx->CurrentBlock = NULL;
  944.       ctx->CurrentListNum = 0;
  945.       ctx->CurrentPos = 0;
  946.  
  947.       ctx->ErrorValue = (GLenum) GL_NO_ERROR;
  948.  
  949.       /* For debug/development only */
  950.       ctx->NoRaster = getenv("MESA_NO_RASTER") ? GL_TRUE : GL_FALSE;
  951.  
  952.       /* Dither disable */
  953.       ctx->NoDither = getenv("MESA_NO_DITHER") ? GL_TRUE : GL_FALSE;
  954.       if (ctx->NoDither) {
  955.          if (getenv("MESA_DEBUG")) {
  956.             fprintf(stderr, "MESA_NO_DITHER set - dithering disabled\n");
  957.          }
  958.          ctx->Color.DitherFlag = GL_FALSE;
  959.       }
  960.    }
  961. }
  962.  
  963.  
  964.  
  965. /*
  966.  * Allocate a new GLvisual object.
  967.  * Input:  rgbFlag - GL_TRUE=RGB(A) mode, GL_FALSE=Color Index mode
  968.  *         alphaFlag - alloc software alpha buffers?
  969.  *         dbFlag - double buffering?
  970.  *         stereoFlag - stereo buffer?
  971.  *         depthFits - requested minimum bits per depth buffer value
  972.  *         stencilFits - requested minimum bits per stencil buffer value
  973.  *         accumFits - requested minimum bits per accum buffer component
  974.  *         indexFits - number of bits per pixel if rgbFlag==GL_FALSE
  975.  *         red/green/blue/alphaFits - number of bits per color component
  976.  *                                     in frame buffer for RGB(A) mode.
  977.  * Return:  pointer to new GLvisual or NULL if requested parameters can't
  978.  *          be met.
  979.  */
  980. GLvisual *gl_create_visual( GLboolean rgbFlag,
  981.                             GLboolean alphaFlag,
  982.                             GLboolean dbFlag,
  983.                             GLboolean stereoFlag,
  984.                             GLint depthBits,
  985.                             GLint stencilBits,
  986.                             GLint accumBits,
  987.                             GLint indexBits,
  988.                             GLint redBits,
  989.                             GLint greenBits,
  990.                             GLint blueBits,
  991.                             GLint alphaBits )
  992. {
  993.    GLvisual *vis;
  994.  
  995.    if (stereoFlag) {
  996.       gl_warning(NULL, "stereo not supported");
  997.       return NULL;   /* Stereo isn't supported yet! */
  998.    }
  999.  
  1000.    if (depthBits > (GLint) (8*sizeof(GLdepth))) {
  1001.       /* can't meet depth buffer requirements */
  1002.       return NULL;
  1003.    }
  1004.    if (stencilBits > (GLint) (8*sizeof(GLstencil))) {
  1005.       /* can't meet stencil buffer requirements */
  1006.       return NULL;
  1007.    }
  1008.    if (accumBits > (GLint) (8*sizeof(GLaccum))) {
  1009.       /* can't meet accum buffer requirements */
  1010.       return NULL;
  1011.    }
  1012.  
  1013.    vis = (GLvisual *) calloc( 1, sizeof(GLvisual) );
  1014.    if (!vis) {
  1015.       return NULL;
  1016.    }
  1017.  
  1018.    vis->RGBAflag   = rgbFlag;
  1019.    vis->DBflag     = dbFlag;
  1020.    vis->StereoFlag = stereoFlag;
  1021.    vis->RedBits    = redBits;
  1022.    vis->GreenBits  = greenBits;
  1023.    vis->BlueBits   = blueBits;
  1024.    vis->AlphaBits  = alphaFlag ? 8*sizeof(GLubyte) : alphaBits;
  1025.  
  1026.    vis->IndexBits   = indexBits;
  1027.    vis->DepthBits   = (depthBits>0) ? 8*sizeof(GLdepth) : 0;
  1028.    vis->AccumBits   = (accumBits>0) ? 8*sizeof(GLaccum) : 0;
  1029.    vis->StencilBits = (stencilBits>0) ? 8*sizeof(GLstencil) : 0;
  1030.  
  1031.    /* software alpha buffers */
  1032.    if (alphaFlag) {
  1033.       vis->FrontAlphaEnabled = GL_TRUE;
  1034.       if (dbFlag) {
  1035.          vis->BackAlphaEnabled = GL_TRUE;
  1036.       }
  1037.    }
  1038.  
  1039.    return vis;
  1040. }
  1041.  
  1042.  
  1043.  
  1044.  
  1045. void gl_destroy_visual( GLvisual *vis )
  1046. {
  1047.    free( vis );
  1048. }
  1049.  
  1050.  
  1051.  
  1052. /*
  1053.  * Allocate the proxy textures.  If we run out of memory part way through
  1054.  * the allocations clean up and return GL_FALSE.
  1055.  * Return:  GL_TRUE=success, GL_FALSE=failure
  1056.  */
  1057. static GLboolean alloc_proxy_textures( GLcontext *ctx )
  1058. {
  1059.    GLboolean out_of_memory;
  1060.    GLint i;
  1061.  
  1062.    ctx->Texture.Proxy1D = gl_alloc_texture_object(NULL, 0, 1);
  1063.    if (!ctx->Texture.Proxy1D) {
  1064.       return GL_FALSE;
  1065.    }
  1066.  
  1067.    ctx->Texture.Proxy2D = gl_alloc_texture_object(NULL, 0, 2);
  1068.    if (!ctx->Texture.Proxy2D) {
  1069.       gl_free_texture_object(NULL, ctx->Texture.Proxy1D);
  1070.       return GL_FALSE;
  1071.    }
  1072.  
  1073.    ctx->Texture.Proxy3D = gl_alloc_texture_object(NULL, 0, 3);
  1074.    if (!ctx->Texture.Proxy3D) {
  1075.       gl_free_texture_object(NULL, ctx->Texture.Proxy1D);
  1076.       gl_free_texture_object(NULL, ctx->Texture.Proxy2D);
  1077.       return GL_FALSE;
  1078.    }
  1079.  
  1080.    out_of_memory = GL_FALSE;
  1081.    for (i=0;i<MAX_TEXTURE_LEVELS;i++) {
  1082.       ctx->Texture.Proxy1D->Image[i] = gl_alloc_texture_image();
  1083.       ctx->Texture.Proxy2D->Image[i] = gl_alloc_texture_image();
  1084.       ctx->Texture.Proxy3D->Image[i] = gl_alloc_texture_image();
  1085.       if (!ctx->Texture.Proxy1D->Image[i]
  1086.           || !ctx->Texture.Proxy2D->Image[i]
  1087.           || !ctx->Texture.Proxy3D->Image[i]) {
  1088.          out_of_memory = GL_TRUE;
  1089.       }
  1090.    }
  1091.    if (out_of_memory) {
  1092.       for (i=0;i<MAX_TEXTURE_LEVELS;i++) {
  1093.          if (ctx->Texture.Proxy1D->Image[i]) {
  1094.             gl_free_texture_image(ctx->Texture.Proxy1D->Image[i]);
  1095.          }
  1096.          if (ctx->Texture.Proxy2D->Image[i]) {
  1097.             gl_free_texture_image(ctx->Texture.Proxy2D->Image[i]);
  1098.          }
  1099.          if (ctx->Texture.Proxy3D->Image[i]) {
  1100.             gl_free_texture_image(ctx->Texture.Proxy3D->Image[i]);
  1101.          }
  1102.       }
  1103.       gl_free_texture_object(NULL, ctx->Texture.Proxy1D);
  1104.       gl_free_texture_object(NULL, ctx->Texture.Proxy2D);
  1105.       gl_free_texture_object(NULL, ctx->Texture.Proxy3D);
  1106.       return GL_FALSE;
  1107.    }
  1108.    else {
  1109.       return GL_TRUE;
  1110.    }
  1111. }
  1112.  
  1113.  
  1114.  
  1115. /*
  1116.  * Allocate and initialize a GLcontext structure.
  1117.  * Input:  visual - a GLvisual pointer
  1118.  *         sharelist - another context to share display lists with or NULL
  1119.  *         driver_ctx - pointer to device driver's context state struct
  1120.  * Return:  pointer to a new gl_context struct or NULL if error.
  1121.  */
  1122. GLcontext *gl_create_context( GLvisual *visual,
  1123.                               GLcontext *share_list,
  1124.                               void *driver_ctx,
  1125.                               GLboolean direct )
  1126. {
  1127.    GLcontext *ctx;
  1128.  
  1129.    /* do some implementation tests */
  1130.    assert( sizeof(GLbyte) == 1 );
  1131.    assert( sizeof(GLshort) >= 2 );
  1132.    assert( sizeof(GLint) >= 4 );
  1133.    assert( sizeof(GLubyte) == 1 );
  1134.    assert( sizeof(GLushort) >= 2 );
  1135.    assert( sizeof(GLuint) >= 4 );
  1136.  
  1137.    /* misc one-time initializations */
  1138.    gl_init_math();
  1139.    gl_init_lists();
  1140.    gl_init_eval();
  1141.  
  1142.    ctx = (GLcontext *) calloc( 1, sizeof(GLcontext) );
  1143.    if (!ctx) {
  1144.       return NULL;
  1145.    }
  1146.  
  1147.    ctx->DriverCtx = driver_ctx;
  1148.    ctx->Visual = visual;
  1149.    ctx->Buffer = NULL;
  1150.  
  1151.    ctx->VB = gl_alloc_vb();
  1152.    if (!ctx->VB) {
  1153.       free( ctx );
  1154.       return NULL;
  1155.    }
  1156.  
  1157.    ctx->PB = gl_alloc_pb();
  1158.    if (!ctx->PB) {
  1159.       free( ctx->VB );
  1160.       free( ctx );
  1161.       return NULL;
  1162.    }
  1163.  
  1164.    if (share_list) {
  1165.       /* share the group of display lists of another context */
  1166.       ctx->Shared = share_list->Shared;
  1167.    }
  1168.    else {
  1169.       /* allocate new group of display lists */
  1170.       ctx->Shared = alloc_shared_state();
  1171.       if (!ctx->Shared) {
  1172.          free(ctx->VB);
  1173.          free(ctx->PB);
  1174.          free(ctx);
  1175.          return NULL;
  1176.       }
  1177.    }
  1178.    ctx->Shared->RefCount++;
  1179.  
  1180.    initialize_context( ctx );
  1181.  
  1182.    ctx->DirectContext = direct;
  1183.  
  1184.    if (visual->DBflag) {
  1185.       ctx->Color.DrawBuffer = GL_BACK;
  1186.       ctx->Pixel.ReadBuffer = GL_BACK;
  1187.    }
  1188.    else {
  1189.       ctx->Color.DrawBuffer = GL_FRONT;
  1190.       ctx->Pixel.ReadBuffer = GL_FRONT;
  1191.    }
  1192.  
  1193. #ifdef PROFILE
  1194.    init_timings( ctx );
  1195. #endif
  1196.  
  1197. #ifdef GL_VERSION_1_1
  1198.    if (!alloc_proxy_textures(ctx)) {
  1199.       free_shared_state(ctx, ctx->Shared);
  1200.       free(ctx->VB);
  1201.       free(ctx->PB);
  1202.       free(ctx);
  1203.       return NULL;
  1204.    }
  1205. #endif
  1206.  
  1207.    gl_init_api_function_pointers( ctx );
  1208.    ctx->API = ctx->Exec;   /* GL_EXECUTE is default */
  1209.  
  1210.    return ctx;
  1211. }
  1212.  
  1213.  
  1214.  
  1215. /*
  1216.  * Destroy a gl_context structure.
  1217.  */
  1218. void gl_destroy_context( GLcontext *ctx )
  1219. {
  1220.    if (ctx) {
  1221.  
  1222. #ifdef PROFILE
  1223.       if (getenv("MESA_PROFILE")) {
  1224.          print_timings( ctx );
  1225.       }
  1226. #endif
  1227.  
  1228.       free( ctx->PB );
  1229.       free( ctx->VB );
  1230.  
  1231.       ctx->Shared->RefCount--;
  1232.       assert(ctx->Shared->RefCount>=0);
  1233.       if (ctx->Shared->RefCount==0) {
  1234.      /* free shared state */
  1235.      free_shared_state( ctx, ctx->Shared );
  1236.       }
  1237.  
  1238.       /* Free proxy texture objects */
  1239.       gl_free_texture_object( NULL, ctx->Texture.Proxy1D );
  1240.       gl_free_texture_object( NULL, ctx->Texture.Proxy2D );
  1241.       gl_free_texture_object( NULL, ctx->Texture.Proxy3D );
  1242.  
  1243.       /* Free evaluator data */
  1244.       if (ctx->EvalMap.Map1Vertex3.Points)
  1245.          free( ctx->EvalMap.Map1Vertex3.Points );
  1246.       if (ctx->EvalMap.Map1Vertex4.Points)
  1247.          free( ctx->EvalMap.Map1Vertex4.Points );
  1248.       if (ctx->EvalMap.Map1Index.Points)
  1249.          free( ctx->EvalMap.Map1Index.Points );
  1250.       if (ctx->EvalMap.Map1Color4.Points)
  1251.          free( ctx->EvalMap.Map1Color4.Points );
  1252.       if (ctx->EvalMap.Map1Normal.Points)
  1253.          free( ctx->EvalMap.Map1Normal.Points );
  1254.       if (ctx->EvalMap.Map1Texture1.Points)
  1255.          free( ctx->EvalMap.Map1Texture1.Points );
  1256.       if (ctx->EvalMap.Map1Texture2.Points)
  1257.          free( ctx->EvalMap.Map1Texture2.Points );
  1258.       if (ctx->EvalMap.Map1Texture3.Points)
  1259.          free( ctx->EvalMap.Map1Texture3.Points );
  1260.       if (ctx->EvalMap.Map1Texture4.Points)
  1261.          free( ctx->EvalMap.Map1Texture4.Points );
  1262.  
  1263.       if (ctx->EvalMap.Map2Vertex3.Points)
  1264.          free( ctx->EvalMap.Map2Vertex3.Points );
  1265.       if (ctx->EvalMap.Map2Vertex4.Points)
  1266.          free( ctx->EvalMap.Map2Vertex4.Points );
  1267.       if (ctx->EvalMap.Map2Index.Points)
  1268.          free( ctx->EvalMap.Map2Index.Points );
  1269.       if (ctx->EvalMap.Map2Color4.Points)
  1270.          free( ctx->EvalMap.Map2Color4.Points );
  1271.       if (ctx->EvalMap.Map2Normal.Points)
  1272.          free( ctx->EvalMap.Map2Normal.Points );
  1273.       if (ctx->EvalMap.Map2Texture1.Points)
  1274.          free( ctx->EvalMap.Map2Texture1.Points );
  1275.       if (ctx->EvalMap.Map2Texture2.Points)
  1276.          free( ctx->EvalMap.Map2Texture2.Points );
  1277.       if (ctx->EvalMap.Map2Texture3.Points)
  1278.          free( ctx->EvalMap.Map2Texture3.Points );
  1279.       if (ctx->EvalMap.Map2Texture4.Points)
  1280.          free( ctx->EvalMap.Map2Texture4.Points );
  1281.  
  1282.       free( (void *) ctx );
  1283.  
  1284. #ifndef THREADS
  1285.       if (ctx==CC) {
  1286.          CC = NULL;
  1287.       }
  1288. #endif
  1289.  
  1290.    }
  1291. }
  1292.  
  1293.  
  1294.  
  1295. /*
  1296.  * Create a new framebuffer.  A GLframebuffer is a struct which
  1297.  * encapsulates the depth, stencil and accum buffers and related
  1298.  * parameters.
  1299.  * Input:  visual - a GLvisual pointer
  1300.  * Return:  pointer to new GLframebuffer struct or NULL if error.
  1301.  */
  1302. GLframebuffer *gl_create_framebuffer( GLvisual *visual )
  1303. {
  1304.    GLframebuffer *buffer;
  1305.  
  1306.    buffer = (GLframebuffer *) calloc( 1, sizeof(GLframebuffer) );
  1307.    if (!buffer) {
  1308.       return NULL;
  1309.    }
  1310.  
  1311.    buffer->Visual = visual;
  1312.  
  1313.    return buffer;
  1314. }
  1315.  
  1316.  
  1317.  
  1318. /*
  1319.  * Free a framebuffer struct and its buffers.
  1320.  */
  1321. void gl_destroy_framebuffer( GLframebuffer *buffer )
  1322. {
  1323.    if (buffer) {
  1324.       if (buffer->Depth) {
  1325.          free( buffer->Depth );
  1326.       }
  1327.       if (buffer->Accum) {
  1328.          free( buffer->Accum );
  1329.       }
  1330.       if (buffer->Stencil) {
  1331.          free( buffer->Stencil );
  1332.       }
  1333.       if (buffer->FrontAlpha) {
  1334.          free( buffer->FrontAlpha );
  1335.       }
  1336.       if (buffer->BackAlpha) {
  1337.          free( buffer->BackAlpha );
  1338.       }
  1339.       free(buffer);
  1340.    }
  1341. }
  1342.  
  1343.  
  1344.  
  1345. /*
  1346.  * Set the current context, binding the given frame buffer to the context.
  1347.  */
  1348. void gl_make_current( GLcontext *ctx, GLframebuffer *buffer )
  1349. {
  1350. #ifdef THREADS
  1351.    /* TODO: unbind old buffer from context? */
  1352.    set_thread_context( ctx );
  1353. #else
  1354.    if (CC && CC->Buffer) {
  1355.       /* unbind frame buffer from context */
  1356.       CC->Buffer = NULL;
  1357.    }
  1358.    CC = ctx;
  1359. #endif
  1360.  
  1361.    if (ctx && buffer) {
  1362.       /* TODO: check if ctx and buffer's visual match??? */
  1363.       ctx->Buffer = buffer;      /* Bind the frame buffer to the context */
  1364.       ctx->NewState = NEW_ALL;   /* just to be safe */
  1365.       gl_update_state( ctx );
  1366.    }
  1367. }
  1368.  
  1369.  
  1370. /*
  1371.  * Return current context handle.
  1372.  */
  1373. GLcontext *gl_get_current_context( void )
  1374. {
  1375. #ifdef THREADS
  1376.    return gl_get_thread_context();
  1377. #else
  1378.    return CC;
  1379. #endif
  1380. }
  1381.  
  1382.  
  1383.  
  1384. /*
  1385.  * Copy attribute groups from one context to another.
  1386.  * Input:  src - source context
  1387.  *         dst - destination context
  1388.  *         mask - bitwise OR of GL_*_BIT flags
  1389.  */
  1390. void gl_copy_context( const GLcontext *src, GLcontext *dst, GLuint mask )
  1391. {
  1392.    if (mask & GL_ACCUM_BUFFER_BIT) {
  1393.       MEMCPY( &dst->Accum, &src->Accum, sizeof(struct gl_accum_attrib) );
  1394.    }
  1395.    if (mask & GL_COLOR_BUFFER_BIT) {
  1396.       MEMCPY( &dst->Color, &src->Color, sizeof(struct gl_colorbuffer_attrib) );
  1397.    }
  1398.    if (mask & GL_CURRENT_BIT) {
  1399.       MEMCPY( &dst->Current, &src->Current, sizeof(struct gl_current_attrib) );
  1400.    }
  1401.    if (mask & GL_DEPTH_BUFFER_BIT) {
  1402.       MEMCPY( &dst->Depth, &src->Depth, sizeof(struct gl_depthbuffer_attrib) );
  1403.    }
  1404.    if (mask & GL_ENABLE_BIT) {
  1405.       /* no op */
  1406.    }
  1407.    if (mask & GL_EVAL_BIT) {
  1408.       MEMCPY( &dst->Eval, &src->Eval, sizeof(struct gl_eval_attrib) );
  1409.    }
  1410.    if (mask & GL_FOG_BIT) {
  1411.       MEMCPY( &dst->Fog, &src->Fog, sizeof(struct gl_fog_attrib) );
  1412.    }
  1413.    if (mask & GL_HINT_BIT) {
  1414.       MEMCPY( &dst->Hint, &src->Hint, sizeof(struct gl_hint_attrib) );
  1415.    }
  1416.    if (mask & GL_LIGHTING_BIT) {
  1417.       MEMCPY( &dst->Light, &src->Light, sizeof(struct gl_light_attrib) );
  1418.    }
  1419.    if (mask & GL_LINE_BIT) {
  1420.       MEMCPY( &dst->Line, &src->Line, sizeof(struct gl_line_attrib) );
  1421.    }
  1422.    if (mask & GL_LIST_BIT) {
  1423.       MEMCPY( &dst->List, &src->List, sizeof(struct gl_list_attrib) );
  1424.    }
  1425.    if (mask & GL_PIXEL_MODE_BIT) {
  1426.       MEMCPY( &dst->Pixel, &src->Pixel, sizeof(struct gl_pixel_attrib) );
  1427.    }
  1428.    if (mask & GL_POINT_BIT) {
  1429.       MEMCPY( &dst->Point, &src->Point, sizeof(struct gl_point_attrib) );
  1430.    }
  1431.    if (mask & GL_POLYGON_BIT) {
  1432.       MEMCPY( &dst->Polygon, &src->Polygon, sizeof(struct gl_polygon_attrib) );
  1433.    }
  1434.    if (mask & GL_POLYGON_STIPPLE_BIT) {
  1435.       /* Use loop instead of MEMCPY due to problem with Portland Group's
  1436.        * C compiler.  Reported by John Stone.
  1437.        */
  1438.       int i;
  1439.       for (i=0;i<32;i++) {
  1440.          dst->PolygonStipple[i] = src->PolygonStipple[i];
  1441.       }
  1442.    }
  1443.    if (mask & GL_SCISSOR_BIT) {
  1444.       MEMCPY( &dst->Scissor, &src->Scissor, sizeof(struct gl_scissor_attrib) );
  1445.    }
  1446.    if (mask & GL_STENCIL_BUFFER_BIT) {
  1447.       MEMCPY( &dst->Stencil, &src->Stencil, sizeof(struct gl_stencil_attrib) );
  1448.    }
  1449.    if (mask & GL_TEXTURE_BIT) {
  1450.       MEMCPY( &dst->Texture, &src->Texture, sizeof(struct gl_texture_attrib) );
  1451.    }
  1452.    if (mask & GL_TRANSFORM_BIT) {
  1453.       MEMCPY( &dst->Transform, &src->Transform, sizeof(struct gl_transform_attrib) );
  1454.    }
  1455.    if (mask & GL_VIEWPORT_BIT) {
  1456.       MEMCPY( &dst->Viewport, &src->Viewport, sizeof(struct gl_viewport_attrib) );
  1457.    }
  1458. }
  1459.  
  1460.  
  1461.  
  1462. /*
  1463.  * Someday a GLS library or OpenGL-like debugger may call this function
  1464.  * to register it's own set of API entry points.
  1465.  * Input: ctx - the context to set API pointers for
  1466.  *        api - if NULL, restore original API pointers
  1467.  *              else, set API function table to this table.
  1468.  */
  1469. void gl_set_api_table( GLcontext *ctx, const struct gl_api_table *api )
  1470. {
  1471.    if (api) {
  1472.       MEMCPY( &ctx->API, api, sizeof(struct gl_api_table) );
  1473.    }
  1474.    else {
  1475.       MEMCPY( &ctx->API, &ctx->Exec, sizeof(struct gl_api_table) );
  1476.    }
  1477. }
  1478.  
  1479.  
  1480.  
  1481.  
  1482. /**********************************************************************/
  1483. /*****                Miscellaneous functions                     *****/
  1484. /**********************************************************************/
  1485.  
  1486.  
  1487. /*
  1488.  * This function is called when the Mesa user has stumbled into a code
  1489.  * path which may not be implemented fully or correctly.
  1490.  */
  1491. void gl_problem( const GLcontext *ctx, const char *s )
  1492. {
  1493.    fprintf( stderr, "Mesa implementation error: %s\n", s );
  1494.    fprintf( stderr, "Report to Mesa author.\n" );
  1495.    (void) ctx;
  1496. }
  1497.  
  1498.  
  1499.  
  1500. /*
  1501.  * This is called to inform the user that he or she has tried to do
  1502.  * something illogical or if there's likely a bug in their program
  1503.  * (like enabled depth testing without a depth buffer).
  1504.  */
  1505. void gl_warning( const GLcontext *ctx, const char *s )
  1506. {
  1507.    GLboolean debug;
  1508. #ifdef DEBUG
  1509.    debug = GL_TRUE;
  1510. #else
  1511.    if (getenv("MESA_DEBUG")) {
  1512.       debug = GL_TRUE;
  1513.    }
  1514.    else {
  1515.       debug = GL_FALSE;
  1516.    }
  1517. #endif
  1518.    if (debug) {
  1519.       fprintf( stderr, "Mesa warning: %s\n", s );
  1520.    }
  1521.    (void) ctx;
  1522. }
  1523.  
  1524.  
  1525.  
  1526. /*
  1527.  * This is Mesa's error handler.  Normally, all that's done is the updating
  1528.  * of the current error value.  If Mesa is compiled with -DDEBUG or if the
  1529.  * environment variable "MESA_DEBUG" is defined then a real error message
  1530.  * is printed to stderr.
  1531.  * Input:  error - the error value
  1532.  *         s - a diagnostic string
  1533.  */
  1534. void gl_error( GLcontext *ctx, GLenum error, const char *s )
  1535. {
  1536.    GLboolean debug;
  1537.  
  1538. #ifdef DEBUG
  1539.    debug = GL_TRUE;
  1540. #else
  1541.    if (getenv("MESA_DEBUG")) {
  1542.       debug = GL_TRUE;
  1543.    }
  1544.    else {
  1545.       debug = GL_FALSE;
  1546.    }
  1547. #endif
  1548.  
  1549.    if (debug) {
  1550.       char errstr[1000];
  1551.  
  1552.       switch (error) {
  1553.      case GL_NO_ERROR:
  1554.         strcpy( errstr, "GL_NO_ERROR" );
  1555.         break;
  1556.      case GL_INVALID_VALUE:
  1557.         strcpy( errstr, "GL_INVALID_VALUE" );
  1558.         break;
  1559.      case GL_INVALID_ENUM:
  1560.         strcpy( errstr, "GL_INVALID_ENUM" );
  1561.         break;
  1562.      case GL_INVALID_OPERATION:
  1563.         strcpy( errstr, "GL_INVALID_OPERATION" );
  1564.         break;
  1565.      case GL_STACK_OVERFLOW:
  1566.         strcpy( errstr, "GL_STACK_OVERFLOW" );
  1567.         break;
  1568.      case GL_STACK_UNDERFLOW:
  1569.         strcpy( errstr, "GL_STACK_UNDERFLOW" );
  1570.         break;
  1571.      case GL_OUT_OF_MEMORY:
  1572.         strcpy( errstr, "GL_OUT_OF_MEMORY" );
  1573.         break;
  1574.      default:
  1575.         strcpy( errstr, "unknown" );
  1576.         break;
  1577.       }
  1578.       fprintf( stderr, "Mesa user error: %s in %s\n", errstr, s );
  1579.    }
  1580.  
  1581.    if (ctx->ErrorValue==GL_NO_ERROR) {
  1582.       ctx->ErrorValue = error;
  1583.    }
  1584.  
  1585.    /* Call device driver's error handler, if any.  This is used on the Mac. */
  1586.    if (ctx->Driver.Error) {
  1587.       (*ctx->Driver.Error)( ctx );
  1588.    }
  1589. }
  1590.  
  1591.  
  1592.  
  1593.  
  1594. GLenum gl_GetError( GLcontext *ctx )
  1595. {
  1596.    GLenum e;
  1597.  
  1598.    if (INSIDE_BEGIN_END(ctx)) {
  1599.       gl_error( ctx, GL_INVALID_OPERATION, "glGetError" );
  1600.       return GL_INVALID_OPERATION;
  1601.    }
  1602.  
  1603.    e = ctx->ErrorValue;
  1604.    ctx->ErrorValue = (GLenum) GL_NO_ERROR;
  1605.    return e;
  1606. }
  1607.  
  1608.  
  1609.  
  1610. void gl_ResizeBuffersMESA( GLcontext *ctx )
  1611. {
  1612.    GLint newsize;
  1613.    GLuint buf_width, buf_height;
  1614.    
  1615.    ctx->NewState |= NEW_ALL;   /* just to be safe */
  1616.  
  1617.    /* ask device driver for size of output buffer */
  1618.    (*ctx->Driver.GetBufferSize)( ctx, &buf_width, &buf_height );
  1619.  
  1620.    /* see if size of device driver's color buffer (window) has changed */
  1621.    newsize = ctx->Buffer->Width != (GLint) buf_width
  1622.           || ctx->Buffer->Height != (GLint) buf_height;
  1623.  
  1624.    /* save buffer size */
  1625.    ctx->Buffer->Width = buf_width;
  1626.    ctx->Buffer->Height = buf_height;
  1627.  
  1628.    /* Reallocate other buffers if needed. */
  1629.    if (newsize && ctx->Visual->DepthBits>0) {
  1630.       /* reallocate depth buffer */
  1631.       (*ctx->Driver.AllocDepthBuffer)( ctx );
  1632.    }
  1633.    if (newsize && ctx->Visual->StencilBits>0) {
  1634.       /* reallocate stencil buffer */
  1635.       gl_alloc_stencil_buffer( ctx );
  1636.    }
  1637.    if (newsize && ctx->Visual->AccumBits>0) {
  1638.       /* reallocate accum buffer */
  1639.       gl_alloc_accum_buffer( ctx );
  1640.    }
  1641.    if (newsize
  1642.        && (ctx->Visual->FrontAlphaEnabled || ctx->Visual->BackAlphaEnabled)) {
  1643.       gl_alloc_alpha_buffers( ctx );
  1644.    }
  1645. }
  1646.  
  1647.  
  1648.  
  1649.  
  1650. /**********************************************************************/
  1651. /*****                   State update logic                       *****/
  1652. /**********************************************************************/
  1653.  
  1654.  
  1655. /*
  1656.  * Since the device driver may or may not support pixel logic ops we
  1657.  * have to make some extensive tests to determine whether or not
  1658.  * software-implemented logic operations have to be used.
  1659.  */
  1660. static void update_pixel_logic( GLcontext *ctx )
  1661. {
  1662.    if (ctx->Visual->RGBAflag) {
  1663.       /* RGBA mode blending w/ Logic Op */
  1664.       if (ctx->Color.ColorLogicOpEnabled) {
  1665.      if (ctx->Driver.LogicOp
  1666.              && (*ctx->Driver.LogicOp)( ctx, ctx->Color.LogicOp )) {
  1667.         /* Device driver can do logic, don't have to do it in software */
  1668.         ctx->Color.SWLogicOpEnabled = GL_FALSE;
  1669.      }
  1670.      else {
  1671.         /* Device driver can't do logic op so we do it in software */
  1672.         ctx->Color.SWLogicOpEnabled = GL_TRUE;
  1673.      }
  1674.       }
  1675.       else {
  1676.      /* no logic op */
  1677.      if (ctx->Driver.LogicOp) {
  1678.             (void) (*ctx->Driver.LogicOp)( ctx, GL_COPY );
  1679.          }
  1680.      ctx->Color.SWLogicOpEnabled = GL_FALSE;
  1681.       }
  1682.    }
  1683.    else {
  1684.       /* CI mode Logic Op */
  1685.       if (ctx->Color.IndexLogicOpEnabled) {
  1686.      if (ctx->Driver.LogicOp
  1687.              && (*ctx->Driver.LogicOp)( ctx, ctx->Color.LogicOp )) {
  1688.         /* Device driver can do logic, don't have to do it in software */
  1689.         ctx->Color.SWLogicOpEnabled = GL_FALSE;
  1690.      }
  1691.      else {
  1692.         /* Device driver can't do logic op so we do it in software */
  1693.         ctx->Color.SWLogicOpEnabled = GL_TRUE;
  1694.      }
  1695.       }
  1696.       else {
  1697.      /* no logic op */
  1698.      if (ctx->Driver.LogicOp) {
  1699.             (void) (*ctx->Driver.LogicOp)( ctx, GL_COPY );
  1700.          }
  1701.      ctx->Color.SWLogicOpEnabled = GL_FALSE;
  1702.       }
  1703.    }
  1704. }
  1705.  
  1706.  
  1707.  
  1708. /*
  1709.  * Check if software implemented RGBA or Color Index masking is needed.
  1710.  */
  1711. static void update_pixel_masking( GLcontext *ctx )
  1712. {
  1713.    if (ctx->Visual->RGBAflag) {
  1714.       GLuint *colorMask = (GLuint *) ctx->Color.ColorMask;
  1715.       if (*colorMask == 0xffffffff) {
  1716.          /* disable masking */
  1717.          if (ctx->Driver.ColorMask) {
  1718.             (void) (*ctx->Driver.ColorMask)( ctx, GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE );
  1719.          }
  1720.          ctx->Color.SWmasking = GL_FALSE;
  1721.       }
  1722.       else {
  1723.          /* Ask driver to do color masking, if it can't then
  1724.           * do it in software
  1725.           */
  1726.          GLboolean red   = ctx->Color.ColorMask[RCOMP] ? GL_TRUE : GL_FALSE;
  1727.          GLboolean green = ctx->Color.ColorMask[GCOMP] ? GL_TRUE : GL_FALSE;
  1728.          GLboolean blue  = ctx->Color.ColorMask[BCOMP] ? GL_TRUE : GL_FALSE;
  1729.          GLboolean alpha = ctx->Color.ColorMask[ACOMP] ? GL_TRUE : GL_FALSE;
  1730.          if (ctx->Driver.ColorMask
  1731.              && (*ctx->Driver.ColorMask)( ctx, red, green, blue, alpha )) {
  1732.             ctx->Color.SWmasking = GL_FALSE;
  1733.          }
  1734.          else {
  1735.             ctx->Color.SWmasking = GL_TRUE;
  1736.          }
  1737.       }
  1738.    }
  1739.    else {
  1740.       if (ctx->Color.IndexMask==0xffffffff) {
  1741.          /* disable masking */
  1742.          if (ctx->Driver.IndexMask) {
  1743.             (void) (*ctx->Driver.IndexMask)( ctx, 0xffffffff );
  1744.          }
  1745.          ctx->Color.SWmasking = GL_FALSE;
  1746.       }
  1747.       else {
  1748.          /* Ask driver to do index masking, if it can't then
  1749.           * do it in software
  1750.           */
  1751.          if (ctx->Driver.IndexMask
  1752.              && (*ctx->Driver.IndexMask)( ctx, ctx->Color.IndexMask )) {
  1753.             ctx->Color.SWmasking = GL_FALSE;
  1754.          }
  1755.          else {
  1756.             ctx->Color.SWmasking = GL_TRUE;
  1757.          }
  1758.       }
  1759.    }
  1760. }
  1761.  
  1762.  
  1763. static void update_fog_mode( GLcontext *ctx )
  1764. {
  1765.    if (ctx->Fog.Enabled) {
  1766.       if (ctx->Texture.Enabled)
  1767.          ctx->FogMode = FOG_FRAGMENT;
  1768.       else if (ctx->Hint.Fog == GL_NICEST)
  1769.          ctx->FogMode = FOG_FRAGMENT;
  1770.       else
  1771.          ctx->FogMode = FOG_VERTEX;
  1772.  
  1773.       if (ctx->Driver.GetParameteri)
  1774.          if ((ctx->Driver.GetParameteri)( ctx, DD_HAVE_HARDWARE_FOG ))
  1775.             ctx->FogMode = FOG_FRAGMENT;
  1776.    }
  1777.    else {
  1778.       ctx->FogMode = FOG_NONE;
  1779.    }
  1780. }
  1781.  
  1782.  
  1783. /*
  1784.  * Recompute the value of ctx->RasterMask, etc. according to
  1785.  * the current context.
  1786.  */
  1787. static void update_rasterflags( GLcontext *ctx )
  1788. {
  1789.    ctx->RasterMask = 0;
  1790.  
  1791.    if (ctx->Color.AlphaEnabled)        ctx->RasterMask |= ALPHATEST_BIT;
  1792.    if (ctx->Color.BlendEnabled)        ctx->RasterMask |= BLEND_BIT;
  1793.    if (ctx->Depth.Test)            ctx->RasterMask |= DEPTH_BIT;
  1794.    if (ctx->FogMode==FOG_FRAGMENT)    ctx->RasterMask |= FOG_BIT;
  1795.    if (ctx->Color.SWLogicOpEnabled)    ctx->RasterMask |= LOGIC_OP_BIT;
  1796.    if (ctx->Scissor.Enabled)        ctx->RasterMask |= SCISSOR_BIT;
  1797.    if (ctx->Stencil.Enabled)        ctx->RasterMask |= STENCIL_BIT;
  1798.    if (ctx->Color.SWmasking)        ctx->RasterMask |= MASKING_BIT;
  1799.    if (ctx->Visual->FrontAlphaEnabled)    ctx->RasterMask |= ALPHABUF_BIT;
  1800.    if (ctx->Visual->BackAlphaEnabled)    ctx->RasterMask |= ALPHABUF_BIT;
  1801.  
  1802.    if (   ctx->Viewport.X<0
  1803.        || ctx->Viewport.X + ctx->Viewport.Width > ctx->Buffer->Width
  1804.        || ctx->Viewport.Y<0
  1805.        || ctx->Viewport.Y + ctx->Viewport.Height > ctx->Buffer->Height) {
  1806.       ctx->RasterMask |= WINCLIP_BIT;
  1807.    }
  1808.  
  1809.    /* check if drawing to front and back buffers */
  1810.    if (ctx->Color.DrawBuffer==GL_FRONT_AND_BACK) {
  1811.       ctx->RasterMask |= FRONT_AND_BACK_BIT;
  1812.    }
  1813.  
  1814.    /* check if writing to color buffer(s) is disabled */
  1815.    if (ctx->Color.DrawBuffer==GL_NONE) {
  1816.       ctx->RasterMask |= NO_DRAW_BIT;
  1817.    }
  1818.    else if (ctx->Visual->RGBAflag && ctx->Color.ColorMask==0) {
  1819.       ctx->RasterMask |= NO_DRAW_BIT;
  1820.    }
  1821.    else if (!ctx->Visual->RGBAflag && ctx->Color.IndexMask==0) {
  1822.       ctx->RasterMask |= NO_DRAW_BIT;
  1823.    }
  1824. }
  1825.  
  1826.  
  1827. /*
  1828.  * Recompute the value of ctx->ClipMask according to the current context.
  1829.  * ClipMask depends on Texturing and Lighting.
  1830.  */
  1831. static void update_clipmask(GLcontext *ctx)
  1832. {
  1833.    /* Recompute ClipMask (what has to be interpolated when clipping) */
  1834.    ctx->ClipMask = 0;
  1835.    if (ctx->Texture.Enabled) {
  1836.       ctx->ClipMask |= CLIP_TEXTURE_BIT;
  1837.    }
  1838.    if (ctx->Light.ShadeModel==GL_SMOOTH) {
  1839.       if (ctx->Visual->RGBAflag) {
  1840.      ctx->ClipMask |= CLIP_FCOLOR_BIT;
  1841.      if (ctx->Light.Model.TwoSide) {
  1842.         ctx->ClipMask |= CLIP_BCOLOR_BIT;
  1843.      }
  1844.       }
  1845.       else {
  1846.      ctx->ClipMask |= CLIP_FINDEX_BIT;
  1847.      if (ctx->Light.Model.TwoSide) {
  1848.         ctx->ClipMask |= CLIP_BINDEX_BIT;
  1849.      }
  1850.       }
  1851.    }
  1852.  
  1853.    if (ctx->Texture.Enabled >= TEXTURE1_1D) {
  1854.       /* Multi texture coords */
  1855.       ctx->ClipInterpFunc = gl_clip_interp_all;
  1856.    }
  1857.    else {
  1858.       switch(ctx->ClipMask) {
  1859.          case CLIP_FCOLOR_BIT | CLIP_TEXTURE_BIT:
  1860.             ctx->ClipInterpFunc = gl_clip_interp_color_tex;
  1861.             break;
  1862.          case CLIP_TEXTURE_BIT:
  1863.             ctx->ClipInterpFunc = gl_clip_interp_tex;
  1864.             break;
  1865.          case CLIP_FCOLOR_BIT:
  1866.             ctx->ClipInterpFunc = gl_clip_interp_color;
  1867.             break;
  1868.          default:
  1869.             ctx->ClipInterpFunc = gl_clip_interp_all;
  1870.       }
  1871.    }
  1872. }
  1873.  
  1874.  
  1875.  
  1876. /*
  1877.  * If ctx->NewState is non-zero then this function MUST be called before
  1878.  * rendering any primitive.  Basically, function pointers and miscellaneous
  1879.  * flags are updated to reflect the current state of the state machine.
  1880.  */
  1881. void gl_update_state( GLcontext *ctx )
  1882. {
  1883.    if (ctx->NewState & NEW_RASTER_OPS) {
  1884.       update_pixel_logic(ctx);
  1885.       update_pixel_masking(ctx);
  1886.       update_fog_mode(ctx);
  1887.       update_rasterflags(ctx);
  1888.       if (ctx->Driver.Dither) {
  1889.          (*ctx->Driver.Dither)( ctx, ctx->Color.DitherFlag );
  1890.       }
  1891.    }
  1892.  
  1893.    if (ctx->NewState & (NEW_RASTER_OPS | NEW_LIGHTING)) {
  1894.       update_clipmask(ctx);
  1895.    }
  1896.  
  1897.    if (ctx->NewState & NEW_LIGHTING) {
  1898.       gl_update_lighting(ctx);
  1899.       gl_set_color_function(ctx);
  1900.    }
  1901.  
  1902.    if (ctx->NewState & NEW_TEXTURING) {
  1903.       gl_update_texture_state(ctx);
  1904.    }
  1905.  
  1906.    if (ctx->NewState & (NEW_LIGHTING | NEW_TEXTURING)) {
  1907.       /* Check if normal vectors are needed */
  1908.       GLboolean sphereGen = GL_FALSE;
  1909.       if (ctx->Texture.Enabled) {
  1910.          GLuint texSet;
  1911.          for (texSet=0; texSet<MAX_TEX_SETS; texSet++) {
  1912.             if ((ctx->Texture.Set[texSet].GenModeS==GL_SPHERE_MAP
  1913.                  && (ctx->Texture.Set[texSet].TexGenEnabled & S_BIT))
  1914.                 || (ctx->Texture.Set[texSet].GenModeT==GL_SPHERE_MAP
  1915.                     && (ctx->Texture.Set[texSet].TexGenEnabled & T_BIT)))
  1916.                sphereGen = GL_TRUE;
  1917.          }
  1918.       }
  1919.       if (ctx->Light.Enabled || sphereGen) {
  1920.          ctx->NeedNormals = GL_TRUE;
  1921.       }
  1922.       else {
  1923.          ctx->NeedNormals = GL_FALSE;
  1924.       }
  1925.    }
  1926.  
  1927.    if (ctx->NewState & NEW_RASTER_OPS) {
  1928.       /* Check if incoming colors can be modified during rasterization */
  1929.       if (ctx->Fog.Enabled ||
  1930.           ctx->Texture.Enabled ||
  1931.           ctx->Color.BlendEnabled ||
  1932.           ctx->Color.SWmasking ||
  1933.           ctx->Color.SWLogicOpEnabled) {
  1934.          ctx->MutablePixels = GL_TRUE;
  1935.       }
  1936.       else {
  1937.          ctx->MutablePixels = GL_FALSE;
  1938.       }
  1939.    }
  1940.  
  1941.    if (ctx->NewState & (NEW_RASTER_OPS | NEW_LIGHTING)) {
  1942.       /* Check if all pixels generated are likely to be the same color */
  1943.       if (ctx->Light.ShadeModel==GL_SMOOTH ||
  1944.           ctx->Light.Enabled ||
  1945.           ctx->Fog.Enabled ||
  1946.           ctx->Texture.Enabled ||
  1947.           ctx->Color.BlendEnabled ||
  1948.           ctx->Color.SWmasking ||
  1949.           ctx->Color.SWLogicOpEnabled) {
  1950.          ctx->MonoPixels = GL_FALSE;       /* pixels probably multicolored */
  1951.       }
  1952.       else {
  1953.          /* pixels will all be same color,
  1954.           * only glColor() can invalidate this.
  1955.           */
  1956.          ctx->MonoPixels = GL_TRUE;
  1957.       }
  1958.    }
  1959.  
  1960.    if (ctx->NewState & NEW_POLYGON) {
  1961.       /* Setup CullBits bitmask */
  1962.       ctx->Polygon.CullBits = 0;
  1963.       if (ctx->Polygon.CullFlag) {
  1964.          if (ctx->Polygon.CullFaceMode==GL_FRONT ||
  1965.              ctx->Polygon.CullFaceMode==GL_FRONT_AND_BACK) {
  1966.             ctx->Polygon.CullBits |= 1;
  1967.          }
  1968.          if (ctx->Polygon.CullFaceMode==GL_BACK ||
  1969.              ctx->Polygon.CullFaceMode==GL_FRONT_AND_BACK) {
  1970.             ctx->Polygon.CullBits |= 2;
  1971.          }
  1972.       }
  1973.       /* Any Polygon offsets enabled? */
  1974.       ctx->Polygon.OffsetAny = ctx->Polygon.OffsetPoint ||
  1975.                                ctx->Polygon.OffsetLine ||
  1976.                                ctx->Polygon.OffsetFill;
  1977.       /* reset Z offsets now */
  1978.       ctx->PointZoffset   = 0.0;
  1979.       ctx->LineZoffset    = 0.0;
  1980.       ctx->PolygonZoffset = 0.0;
  1981.    }
  1982.  
  1983.    if (ctx->NewState & (NEW_POLYGON | NEW_LIGHTING)) {
  1984.       /* Determine if we can directly call the triangle rasterizer */
  1985.       if (   ctx->Polygon.Unfilled
  1986.           || ctx->Polygon.OffsetAny
  1987.           || ctx->Polygon.CullFlag
  1988.           || ctx->Light.Model.TwoSide
  1989.           || ctx->RenderMode!=GL_RENDER) {
  1990.          ctx->DirectTriangles = GL_FALSE;
  1991.       }
  1992.       else {
  1993.          ctx->DirectTriangles = GL_TRUE;
  1994.       }
  1995.    }
  1996.  
  1997.    /* update scissor region */
  1998.    ctx->Buffer->Xmin = 0;
  1999.    ctx->Buffer->Ymin = 0;
  2000.    ctx->Buffer->Xmax = ctx->Buffer->Width-1;
  2001.    ctx->Buffer->Ymax = ctx->Buffer->Height-1;
  2002.    if (ctx->Scissor.Enabled) {
  2003.       if (ctx->Scissor.X > ctx->Buffer->Xmin) {
  2004.          ctx->Buffer->Xmin = ctx->Scissor.X;
  2005.       }
  2006.       if (ctx->Scissor.Y > ctx->Buffer->Ymin) {
  2007.          ctx->Buffer->Ymin = ctx->Scissor.Y;
  2008.       }
  2009.       if (ctx->Scissor.X + ctx->Scissor.Width - 1 < ctx->Buffer->Xmax) {
  2010.          ctx->Buffer->Xmax = ctx->Scissor.X + ctx->Scissor.Width - 1;
  2011.       }
  2012.       if (ctx->Scissor.Y + ctx->Scissor.Height - 1 < ctx->Buffer->Ymax) {
  2013.          ctx->Buffer->Ymax = ctx->Scissor.Y + ctx->Scissor.Height - 1;
  2014.       }
  2015.    }
  2016.  
  2017.    /*
  2018.     * Update Device Driver interface
  2019.     */
  2020.    if (ctx->NewState & NEW_RASTER_OPS) {
  2021.       ctx->Driver.AllocDepthBuffer = gl_alloc_depth_buffer;
  2022.       if (ctx->Depth.Mask) {
  2023.          switch (ctx->Depth.Func) {
  2024.             case GL_LESS:
  2025.                ctx->Driver.DepthTestSpan = gl_depth_test_span_less;
  2026.                ctx->Driver.DepthTestPixels = gl_depth_test_pixels_less;
  2027.                break;
  2028.             case GL_GREATER:
  2029.                ctx->Driver.DepthTestSpan = gl_depth_test_span_greater;
  2030.                ctx->Driver.DepthTestPixels = gl_depth_test_pixels_greater;
  2031.                break;
  2032.             default:
  2033.                ctx->Driver.DepthTestSpan = gl_depth_test_span_generic;
  2034.                ctx->Driver.DepthTestPixels = gl_depth_test_pixels_generic;
  2035.          }
  2036.       }
  2037.       else {
  2038.          ctx->Driver.DepthTestSpan = gl_depth_test_span_generic;
  2039.          ctx->Driver.DepthTestPixels = gl_depth_test_pixels_generic;
  2040.       }
  2041.       ctx->Driver.ReadDepthSpanFloat = gl_read_depth_span_float;
  2042.       ctx->Driver.ReadDepthSpanInt = gl_read_depth_span_int;
  2043.    }
  2044.  
  2045.    ctx->Driver.PointsFunc = NULL;
  2046.    ctx->Driver.LineFunc = NULL;
  2047.    ctx->Driver.TriangleFunc = NULL;
  2048.    ctx->Driver.QuadFunc = NULL;
  2049.    ctx->Driver.RectFunc = NULL;
  2050.  
  2051.    /*
  2052.     * The ctx->Driver.UpdateState pointer _MUST_ be valid at this point
  2053.     * in order for any impending rendering to succeed.
  2054.     */
  2055.    assert(ctx->Driver.UpdateState);
  2056.  
  2057.    /*
  2058.     * Here the driver sets up all the ctx->Driver function pointers to
  2059.     * it's specific, private functions.
  2060.     */
  2061.    (*ctx->Driver.UpdateState)(ctx);
  2062.  
  2063.    /*
  2064.     * In case the driver didn't hook in an optimized point, line or
  2065.     * triangle function we'll now select "core/fallback" point, line
  2066.     * and triangle functions.
  2067.     */
  2068.    gl_set_point_function(ctx);
  2069.    gl_set_line_function(ctx);
  2070.    gl_set_triangle_function(ctx);
  2071.    gl_set_quad_function(ctx);
  2072.  
  2073.    gl_set_vertex_function(ctx);
  2074.  
  2075.    ctx->NewState = 0;
  2076. }
  2077.  
  2078.